package zhr.store.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import zhr.store.common.Result;
import zhr.store.domain.Employee;
import zhr.store.service.EmployeeService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public Result<Employee> login(@RequestBody Employee employee, HttpServletRequest request) {
        //1.将输入的密码进行MD5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2.根据员工提交的账号密码查询数据库是否存在,查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);
        //3.判断是否查询到用户名,查询数据库
        if (emp == null) {
            return Result.error("管理员不存在");
        }
        //4.进行密码比对,查询数据库
        if (!emp.getPassword().equals(password)) {
            return Result.error("密码错误");
        }
        //5.查看员工状态,查询数据库
        if (emp.getStatus() == 0) {
            return Result.error("员工账号已禁用");
        }
        //6.登录成功,将员工id存入session中
        HttpSession session = request.getSession();
        session.setAttribute("employee", emp.getId());
        return Result.success(emp);
    }

    /**
     * 管理员账号退出
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout(HttpServletRequest request) {
        //清理session中保存的id
        request.getSession().removeAttribute("employee");
        return Result.success("退出成功");
    }

    /**
     * 分页查询员工信息
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result<Page> showEmployee(int page, int pageSize, String name) {
        //构建分页构造器
        Page pageShow = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        //构建查询条件
        if (name != null) {
            wrapper.like(Employee::getName, name);
        }
        //添加排序条件
        wrapper.orderByDesc(Employee::getUpdateTime);
        //执行查询
        employeeService.page(pageShow, wrapper);
        return Result.success(pageShow);
    }

    /**
     * 根据id查询员工信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Employee> selectById(@PathVariable Long id) {
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        //构建查询条件
        wrapper.eq(Employee::getId, id);
        //执行查询
        Employee employee = employeeService.getOne(wrapper);
        return Result.success(employee);
    }

    /**
     * 根据id修改员工状态
     *
     * @param employee
     * @return
     */
    @PutMapping
    public Result<String> updateEmployee(@RequestBody Employee employee) {
        //执行修改
        employeeService.updateById(employee);
        return Result.success("员工信息修改成功");
    }

    /**
     * 添加员工
     *
     * @param employee
     * @return
     */
    @PostMapping
    public Result<String> insertEmployee(@RequestBody Employee employee) {
        //1.设置初始密码并进行加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //2.进行添加员工操作
        employeeService.save(employee);
        return Result.success("添加员工成功");
    }
}
